/*
 * @Date: 2023-03-15 14:50:09
 * @LastEditTime: 2023-04-03 17:02:12
 * @FilePath: /NLK/src/store/useUserinfoStore.ts
 * 介绍:
 */
import { defineStore } from "pinia";
import { apiUserinfo, Userinfo } from "@@/api/module/userinfo";
import { sdkWalletPlatformInfo } from "@@/sdk/tp";

export default defineStore("StoreUserinfo", {
  //*全局仓库
  state: initState,
  //*全局函数
  actions: {
    async getUserinfo() {
      const res = await apiUserinfo();
      if (!res) return;
      this.$patch((state) => {
        state.userinfo = res;
      });
    },
    async getWalletName() {
      const res = await sdkWalletPlatformInfo();
      return (this.walletName = res.data?.name || "NLK user");
    },
  },
  //*计算属性
  getters: {},
  //*仓库数据缓存
  persist: [
    {
      key: "StateUserinfo",
      storage: localStorage, //指定储存位置
    },
  ],
});
/**state类型定义 */
interface StoreUserinfo {
  userinfo: Userinfo.Res | undefined;
  walletName: string | undefined;
}
/**初始化pinia */
function initState(): StoreUserinfo {
  return {
    userinfo: undefined,
    walletName: undefined,
  };
}
